﻿using System.Configuration;
using ServiceStack.Redis;
using System.Collections.Generic;
using System;
using KeaoBase;

namespace KeaoReportService
{
    /// <summary>
    /// RedisClientPool 的摘要说明
    /// </summary>
    public static class RedisClientPool
    {
        private static PooledRedisClientManager m_manager;

        public static PooledRedisClientManager m_Manager
        {
            get
            {
                if (m_manager == null)
                {
                    m_manager = new PooledRedisClientManager(new string[] { string.Format("{2}@{0}:{1}",KeaoUtils.ConfigGetNodeAttr("缓存","IP"),KeaoUtils.ConfigGetNodeAttr("缓存","Port"), KeaoUtils.ConfigGetNodeAttr("缓存","Pwd")) });
                    m_manager.ConnectTimeout = 1000;
                }
                return m_manager;
            }
        }

        public static IRedisClient GetRedisClient()
        {
            try
            {
                IRedisClient redis = m_Manager.GetClient();
                return redis;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("获取缓存器异常。{0}", err.Message));
                return null;
            }
        }

        public static IRedisClient GetReadOnlyRedisClient()
        {
            try
            {
                IRedisClient redis = m_Manager.GetReadOnlyClient();
                DateTime serverTime = redis.GetServerTime();
                return redis;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("获取只读缓存器异常。{0}", err.Message));
                return null;
            }
        }
    }
}